package org.lwz.space.controller;

import org.lwz.space.model.Person;
import org.lwz.space.model.User;
import org.lwz.space.service.PersonService;
import org.lwz.space.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoQuan on 2014/12/21.
 */
//..../myzone/user/test
//..../myzone/user/te

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;


    @RequestMapping("/test")
    public String toIndex() {
        return "test";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
        return "login";
    }

    @RequestMapping(value = "/loginSubmit", method = RequestMethod.POST)
    public String loginSubmit(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        final User user = this.userService.getUser(username,password);

        if(user != null){
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            model.addAttribute("user", new ArrayList<User>() {{
                add(user);
            }});
            model.addAttribute("from", "index");
            return "redirect:../article/articles";
        }
        return "login";
    }
}
